<?php




/**
* Edit an entity in [ categories ]
*/

$categories = new categories();

# Handle Editing, when data is supplied
if($variable->post('edit-action', 'string', '') && ($category_id = $variable->post('category_id', 'integer', 0)))
{
	# Editing....
	$code = $variable->post('protection_code', 'string', '');
	$data = $variable->post('categories', 'array', array());
	
	# Mark when this data was modified last time.
	$data['modified_on'] = 'UNIX_TIMESTAMP(CURRENT_TIMESTAMP())';
	$data['modified_counter'] = 'modified_counter+1';

	if($success = $categories->edit(
		$data, # Posted data
		array(
			'category_id' => $category_id,
		),
		$code, # Security code related to this entry
		$category_id
	))
	{
		$tags = new tags();
		$tags->correct_category_tags($category_id, $data['category_keywords']);
		
		$messenger = new messenger('success', 'The record has been modified.');
		
		stopper::url(url::last_page('categories-list.php?context=permissions'));
	}
	else
	{
		stopper::url('categories-edit-error.php');
	}
}
else
{
	/**
	* Otherwise, load the details of the entity before editing it.
	*/
	if($category_id = $variable->get('id', 'integer', 0))
	{
		$details = $categories->details($category_id);
		if(!$details)
		{
			# Data about this entity was not available
			stopper::url('categories-edit-error.php?context=data');
		}
		# Purpose of this code block is to make sure that the variable
		# gets all indices with blank data, to feed to EDIT form.
		$details = $categories->validate('edit', $details);

		/**
		* Build Smarty Variable with FULL details
		*/
		$smarty->assign('categories', $details);
	}
	else
	{
		# Really Bad...
		stopper::url('categories-direct-access-error.php');
	}
}
?>